package org.tinygroup.channel.protocol.impl;

import java.util.List;

import org.tinygroup.channel.protocol.ChannelClient;
import org.tinygroup.channel.protocol.ProtocolListener;
import org.tinygroup.channel.protocol.ProtocolOutInterface;
import org.tinygroup.context.Context;
import org.tinygroup.context.util.ContextFactory;

public  class BaseProtocolOut<Request, Response> extends
		AbstractProtocolInterface<Request, Response> implements
		ProtocolOutInterface<Request, Response>{
	private ChannelClient<Request, Response> channelClient;
	
	public Response send(Request r){
		List<ProtocolListener<?,?>> listeners = getListeners();
		ProtocolListenerChain chain = new ProtocolListenerChain(listeners);
		Context context = ContextFactory.getContext();
		chain.pre(r, context);
		Response response = channelClient.send(r);
		chain.post(r,response, context);
		return response;
	}

	public void asynSend(Request r) {
		List<ProtocolListener<?,?>> listeners = getListeners();
		ProtocolListenerChain chain = new ProtocolListenerChain(listeners);
		Context context = ContextFactory.getContext();
		chain.pre(r, context);
		channelClient.asynSend(r);
		chain.post(r,null, context);
	}

	public void setChannelClient(ChannelClient<Request, Response> client) {
		this.channelClient = client;
	}

	public ChannelClient<Request, Response> getChannelClient() {
		return channelClient;
	}

}
